from selenium.webdriver.support.ui import WebDriverWait
import time
from selenium.webdriver.support.select import Select

# 封装需要使用的方法
class Base1():
    def __init__(self,driver):
        self.driver=driver

    def open_url(self,url):
        self.driver.get(url)

    def error_png(self):
        error_png=time.strftime('%Y%m%d-%H%M%S')
        return error_png

    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            self.driver.save_screenshot('../Png/'+self.error_png()+'.png')
            print(e)

    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)

    def click(self,loc):
        self.find_element(loc).click()

    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

    def scrool_x(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')

    def scroll_Height(self):
        self.driver.execute_script(f'window.scrollTo(0,document.body.scrollHeight)')

    def select_by_value(self,loc,info):
        value=Select(self.find_element(loc))
        value.select_by_value(info)

    def select_by_indext(self,loc,num):
        index=Select(self.find_element(loc))
        index.select_by_index(num)

    def select_by_visible_text(self,loc,info):
        text=Select(self.find_element(loc))
        text.deselect_by_visible_text(info)





